<?php

class mobiscrollJs extends JsPlugin{
    
    public $file_sample = '';
    
    static private $instance;
    public static function getInstanceOf($plugin){
        $class_name = __CLASS__;
        if (!isset(self::$instance))self::$instance = new $class_name($plugin);
        return self::$instance;
    }
    
    public function init() {
        
        $this->Html->LoadJs("$this->url/js/mobiscroll-2.2.custom.min");
        $this->Html->LoadJs("$this->url/js/br");
        $this->Html->LoadCss("plugins/formulario/mobiscroll");
    }
    

    public function datetime($camp){
        if($camp == ""){throw new resourceException(__CLASS__, "Campo não pode ser uma string vazia");}
        
        $this->Html->LoadJQueryFunction("
        $('form #$camp').scroller({
            preset: 'datetime',
            lang: 'br',
            theme: 'default',
            display: 'modal',
            animate: 'slidehorizontal',
            mode: 'mixed',
            showNow: true,
            rows: '5',
            $this->events
        });");
    }
    
    public function time($camp){
        if($camp == ""){throw new resourceException(__CLASS__, "Campo não pode ser uma string vazia");}
        $this->Html->LoadJQueryFunction(
        "$('#$camp').scroller({
            preset: 'time',
            lang: 'br',
            theme: 'default',
            display: 'modal',
            animate: 'slidehorizontal',
            mode: 'mixed',
            showNow: true,
            rows: '5',
            $this->events
        });");
        
    }
    
    public function date($camp){
        if($camp == ""){throw new resourceException(__CLASS__, "Campo não pode ser uma string vazia");}
        $this->Html->LoadJQueryFunction(
        "$('#$camp').scroller({
            preset: 'date',
            lang: 'br',
            theme: 'default',
            display: 'modal',
            animate: 'slidehorizontal',
            mode: 'mixed',
            showNow: true,
            rows: '5',
            $this->events
        });");
        
    }
    
    public function draw($camp, $mindate = "", $maxdate = ''){
        $this->date($camp, $mindate, $maxdate);
    }
    
    private $events = "";
    public function setEvents($camp, $mindate, $maxdate){
        $events = "";
        if($mindate != ""){
            $var = $this->selectType($mindate);
            $events .= "$('#$camp').scroller('option', 'minDate', $var); ";
        }

        if($maxdate != ""){
            $var = $this->selectType($maxdate);
            $events .= "$('#$camp').scroller('option', 'maxDate', $var);";
        }
        
        if($events != ""){
            $this->events = "
            onBeforeShow: function(valueText, inst) {
                $events
            }";
        }
    }
    
    public function clearEvents(){
        $this->events = '';
    }
    
    private function selectType($date){
        $cal = new calendarEspecial();
        if($cal->isValidDate($date) || $cal->isValidDateTime($date))
            return "new Date('$date')"; 
        if($date == 'now')return "new Date()";
        return "$('#$date').scroller('getDate')";
    }
    
}

?>